##### Figure A1 Script#######

rm(list=ls());gc();gc();gc();gc();gc();gc();gc();gc()

library(tidyverse)
library(lubridate)
library(hrbrthemes)
library(scales)
library(RColorBrewer)
library(viridis)
library(dplyr)


here::i_am("Scripts/FigA1Script.R")

library(here)


#############################################################################
#Number of Candidates in each Election Year by State, no disaggregation
#############################################################################


#load candidacy data, by election cycle 

#KY:

kyc<-readRDS(here("Data", "KY_assembled_candidates.rds"))


kyc$year2<-ifelse(!is.na(kyc$year), kyc$year, kyc$Election_Year)

kycands<-kyc %>%
  group_by(year2)%>%
  dplyr::summarize(numcands=n())

colnames(kycands)[1]<-"year"

kycands$state<-"KY"

#MI
mic<-readRDS(here("Data","MI_assembled_candidates.rds"))


micands<-mic %>%
  group_by(year)%>%
  dplyr::summarize(numcands=n())
micands$state<-"MI"


#OH
ohc<-readRDS(here("Data","OH_assembled_candidates.rds"))

ohc<-ohc%>%
  filter(!grepl("REPRESENTATIVES", ohc$Office_Sought2))


ohcands<-ohc %>%
  group_by(year)%>%
  dplyr::summarize(numcands=n())
ohcands$state<-"OH"

#WV

wvc<-readRDS(here("Data","WV_assembled_candidates.rds"))

wvc$year<-ifelse(!is.na(wvc$Election_Year.ftmcands), wvc$Election_Year.ftmcands, 
                 ifelse(!is.na(wvc$elecyear.wvelec.gen),wvc$elecyear.wvelec.gen, wvc$elecyear.wvelec.pri))


wvcands<-wvc %>%
  filter(year<2019) %>%
  group_by(year)%>%
  dplyr::summarize(numcands=n())
wvcands$state<-"WV"


#Combine these

allcands<-rbind(kycands, micands,ohcands,wvcands)
#saveRDS(allcands, "candnumsallstates.rds")

#Start at 1997:
#allcands<-readRDS("candnumsallstates.rds")

allcands1997<-allcands %>%
  filter(year>1996)

allcands1997$year2<-as.Date(paste(as.character(allcands1997$year), "01", "01", sep="-"))

#Get a color palette of four distinct colors:

colorpal<-brewer.pal(4, "Set1")

ggplot(data=allcands1997, aes(x=year2)) +
  geom_point( aes(y=numcands, color=state), size=6, alpha=.8) + 
  scale_y_continuous(
    # Features of the first axis
    name = "Number of Candidates",
    
    # Add a second axis and specify its features
    labels=comma
  ) + 
  theme_ipsum() +
  theme(
    axis.text.x=element_text(size=22),
    axis.text.y=element_text(size=22),
    axis.title.y = element_text(size=20),
    axis.title.x = element_text(size=20),
    legend.text=element_text(size=20),
    legend.title=element_text(size=20)
  ) +
  scale_x_date(name="Year",date_breaks = "2 years",             
               date_labels = "%Y")+
  labs(colour="State")+
  scale_color_manual(values=colorpal)+
  geom_vline(xintercept = as.numeric(as.Date("1999-01-01")), linetype="dotted")+
  geom_vline(xintercept = as.numeric(as.Date("2012-01-01")), linetype="dotted")+
  geom_vline(xintercept = as.numeric(as.Date("2006-01-01")), linetype="dotted")+
  geom_vline(xintercept = as.numeric(as.Date("2002-01-01")), linetype="dotted")+
  ggplot2::annotate(geom="text", x=as.Date("1999-01-01"), y=750, size=6,
           label="Statewide KY \n (Every 4)") +
  ggplot2::annotate(geom="text", x=as.Date("2012-01-01"), y=750, size=6,
           label="Statewide WV \n (Every 4)") +
  ggplot2::annotate(geom="text", x=as.Date("2006-01-01"), y=750, size=6,
           label="Statewide OH \n (Every 4)") +
  ggplot2::annotate(geom="text", x=as.Date("2002-01-01"), y=750, size=6,
           label="Statewide/Senate MI \n (Every 4)") 




ggsave(here("Results","FigA1.png"), height=12, width=20)
